package com.beemans.corekit;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.integration.admob.AbAdvertManager;
import com.beemans.corekit.integration.language.MultiLanguages;
import com.beemans.corekit.integration.polling.LocalPolling;
import com.beemans.corekit.integration.toaster.Toaster;
import com.beemans.corekit.integration.toolkit.AppUtils;
import com.beemans.corekit.integration.topon.AdvertManager;
import com.beemans.corekit.localpush.InstalledAppInfo;
import com.beemans.corekit.localpush.NotificationCallback;
import com.beemans.corekit.localpush.NotificationScheduler;
import com.beemans.corekit.localpush.PackageChangeListener;
import com.beemans.corekit.localpush.PackageEventType;
import com.beemans.corekit.localpush.PackageWatcher;

import java.util.Objects;

public class MyApp extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        try {
            super.attachBaseContext((base != null) ? MultiLanguages.attach(base) : null);
        } catch (Throwable exception) {
            super.attachBaseContext(base);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AppUtils.init(this);

        runMainProcess();
    }

    private void runMainProcess() {
        if (!Objects.equals(Application.getProcessName(), getPackageName())) {
            return;
        }

        // 吐司初始化
        Toaster.init(this);
        // 多语言框架初始化
        MultiLanguages.init(this);
        // Admob 聚合初始化
        AbAdvertManager.init(this);
        // TopOn 初始化
        AdvertManager.init(this, AdvertConstants.TP_APP_ID, AdvertConstants.TP_APP_KEY, BuildConfig.DEBUG);
        // 本地轮询
        LocalPolling.build()
                .setCheckInterval(15 * 1000)
                .setListener(isActionUserPresent -> {
//                    Log.e("susu", "LocalPolling --- isActionUserPresent:" + isActionUserPresent);
                })
                .create();

        PackageWatcher.getInstance(this).setPackageChangeListener(new PackageChangeListener() {
            @Override
            public void onPackageInstalled(@Nullable InstalledAppInfo appInfo) {
                if (appInfo != null) {
                    Log.e("susu", "onPackageInstalled --- packageName:" + appInfo.packageName);
                }
            }

            @Override
            public void onPackageUninstalled(@Nullable InstalledAppInfo appInfo) {
                if (appInfo != null) {
                    Log.e("susu", "onPackageUninstalled --- packageName:" + appInfo.packageName);
                }
            }
        });
        PackageWatcher.getInstance(this).init();

        NotificationScheduler.getInstance(this).setNotificationCallback(new NotificationCallback() {
            @Override
            public void onShowLocalNotification() {
                Log.e("susu", "onShowLocalNotification");
            }

            @Override
            public void onShowListenerNotification() {
                Log.e("susu", "onShowListenerNotification");
            }

            @Override
            public void onShowPackageWatchNotification(@Nullable InstalledAppInfo appInfo, @NonNull PackageEventType type) {
                Log.e("susu", "onShowPackageWatchNotification");
            }
        });
    }
}